<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Scala学习笔记之正则表达式 - (power up)</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="Kevin Jiang" /><meta name="description" content="QQ: 380800878, 微信: kittenll 如果还不熟悉正则表达式的可以先查看正则表达式30分钟入门教程，非常经典的入门教程。 Scala提供的正则表达式功能是通过类Rege" /><meta name="keywords" content="KevinJiang, AI大模型, AI落地, 全栈工程师, Java, Spring Boot" />






<meta name="generator" content="Hugo 0.84.4 with theme even" />


<link rel="canonical" href="http://kevinjiang.info/post/java/scala%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.78f8f17bab244b9ee62ad16480c9584d5fc2db06ae20681d1ca225cefd80767c.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="Scala学习笔记之正则表达式" />
<meta property="og:description" content="QQ: 380800878, 微信: kittenll 如果还不熟悉正则表达式的可以先查看正则表达式30分钟入门教程，非常经典的入门教程。 Scala提供的正则表达式功能是通过类Rege" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://kevinjiang.info/post/java/scala%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2016-08-23T15:27:00+00:00" />
<meta property="article:modified_time" content="2016-08-23T15:27:00+00:00" />

<meta itemprop="name" content="Scala学习笔记之正则表达式">
<meta itemprop="description" content="QQ: 380800878, 微信: kittenll 如果还不熟悉正则表达式的可以先查看正则表达式30分钟入门教程，非常经典的入门教程。 Scala提供的正则表达式功能是通过类Rege"><meta itemprop="datePublished" content="2016-08-23T15:27:00+00:00" />
<meta itemprop="dateModified" content="2016-08-23T15:27:00+00:00" />
<meta itemprop="wordCount" content="1259">
<meta itemprop="keywords" content="Scala,Scala正则表达式," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Scala学习笔记之正则表达式"/>
<meta name="twitter:description" content="QQ: 380800878, 微信: kittenll 如果还不熟悉正则表达式的可以先查看正则表达式30分钟入门教程，非常经典的入门教程。 Scala提供的正则表达式功能是通过类Rege"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">(Power up)</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/resume/">
        <li class="mobile-menu-item">简历</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">(Power up)</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/resume/">简历</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">Scala学习笔记之正则表达式</h1>

      <div class="post-meta">
        <span class="post-time"> 2016-08-23 </span>
        
          <span class="more-meta"> 约 1259 字 </span>
          <span class="more-meta"> 预计阅读 3 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents"></nav>
  </div>
</div>
    <div class="post-content">
      <p>QQ: 380800878, 微信: kittenll</p>
<p>如果还不熟悉正则表达式的可以先查看<a href="http://deerchao.net/tutorials/regex/regex.htm">正则表达式30分钟入门教程</a>，非常经典的入门教程。</p>
<p>Scala提供的正则表达式功能是通过类<code>Regex</code>来实现，函数签名为：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-scala" data-lang="scala"><span class="nc">Regex</span><span class="o">(</span><span class="n">regex</span><span class="k">:</span> <span class="kt">String</span><span class="o">,</span> <span class="n">groupNames</span><span class="k">:</span> <span class="kt">String*</span><span class="o">)</span>
</code></pre></td></tr></table>
</div>
</div><p>第一个参数为正则表达式的字符串，后面是可变参数列表，是给regex里面的分组起一个名字。Scala的正则表达式的功能非常强大，这要得益于Scala本身特别的设计，尤其是pattern match，是其他编程语言很难超越的。</p>
<p>先来看第一个例子，匹配小写英文字母（如下代码中单独一行的=&gt;表示接下来是结果）:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">scala.util.matching.Regex</span>
<span class="k">val</span> <span class="n">str</span> <span class="k">=</span> <span class="s">&#34;abc&#34;</span>
<span class="k">val</span> <span class="n">regex</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">Regex</span><span class="o">(</span><span class="s">&#34;[a-z]+&#34;</span><span class="o">)</span>
<span class="n">println</span><span class="o">(</span><span class="n">regex</span><span class="o">.</span><span class="n">findAllIn</span><span class="o">(</span><span class="n">str</span><span class="o">).</span><span class="n">toList</span><span class="o">)</span>

<span class="k">=&gt;</span>
<span class="nc">List</span><span class="o">(</span><span class="n">abc</span><span class="o">)</span>
</code></pre></td></tr></table>
</div>
</div><p>可以结果看到是一个元素的的List，匹配到字符串abc。如果要匹配到大写和小写，正则表达式需要修改成<code>[a-zA-Z]</code>，还有另外一个办法就是为正则表达式添加选项，让<code>[a-z]</code>可以直接匹配到大写和小写字母，正则表达式就需要写成<code>(?i)[a-z]+</code></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">scala.util.matching.Regex</span>
<span class="k">val</span> <span class="n">str</span> <span class="k">=</span> <span class="s">&#34;abcEFGhijk&#34;</span>
<span class="k">val</span> <span class="n">regex</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">Regex</span><span class="o">(</span><span class="s">&#34;(?i)[a-z]+&#34;</span><span class="o">)</span>
<span class="n">println</span><span class="o">(</span><span class="n">regex</span><span class="o">.</span><span class="n">findAllIn</span><span class="o">(</span><span class="n">str</span><span class="o">).</span><span class="n">toList</span><span class="o">)</span>

<span class="k">=&gt;</span>
<span class="nc">List</span><span class="o">(</span><span class="n">abcEFGhijk</span><span class="o">)</span>
</code></pre></td></tr></table>
</div>
</div><p>所以在Scala添加选项的正确姿势就是在正则表达式的前面用使用括号问号+选项字母，这与PHP的正则完全不同，PHP使用如下的方式，使用相同的字符来做开头和结尾，这里就是<code>/</code>，其实在PHP里面使用<code>%</code>, <code>|</code>这些字符也是完全可以的，然后在后面的分割符后面加上选项字母</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">/[a-z]+/i
</code></pre></td></tr></table>
</div>
</div><p>匹配简单的电话号码：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">scala.util.matching.Regex</span>
<span class="k">val</span> <span class="n">str</span> <span class="k">=</span> <span class="s">&#34;13907461234&#34;</span>
<span class="k">val</span> <span class="n">regex</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">Regex</span><span class="o">(</span><span class="s">&#34;&#34;&#34;^1[358]\d{9}$&#34;&#34;&#34;</span><span class="o">)</span>
<span class="n">println</span><span class="o">(</span><span class="n">regex</span><span class="o">.</span><span class="n">findAllIn</span><span class="o">(</span><span class="n">str</span><span class="o">).</span><span class="n">toList</span><span class="o">)</span>

<span class="k">=&gt;</span>
<span class="nc">List</span><span class="o">(</span><span class="mi">13907461234</span><span class="o">)</span>
</code></pre></td></tr></table>
</div>
</div><p>这里使用了<code>&quot;&quot;&quot;</code>raw string，三个双引号括起来的内容不需要做转义，正则表达式本身就比较难看，如果再加上转义就更难于阅读和理解。</p>
<p>匹配简单的电子邮件：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-scala" data-lang="scala"><span class="k">val</span> <span class="n">str</span> <span class="k">=</span> <span class="s">&#34;email: abc@xyz.com&#34;</span>
<span class="k">val</span> <span class="n">regex</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">Regex</span><span class="o">(</span><span class="s">&#34;&#34;&#34;(?i)[a-z0-9._-]+@[a-z0-9._-]+(\.[a-z0-9._-]+)+&#34;&#34;&#34;</span><span class="o">)</span>
<span class="n">println</span><span class="o">(</span><span class="n">regex</span><span class="o">.</span><span class="n">findAllIn</span><span class="o">(</span><span class="n">str</span><span class="o">).</span><span class="n">toList</span><span class="o">)</span>

<span class="k">=&gt;</span>
<span class="nc">List</span><span class="o">(</span><span class="n">abc</span><span class="nd">@xyz</span><span class="o">.</span><span class="n">com</span><span class="o">)</span>
</code></pre></td></tr></table>
</div>
</div><p>Scala的正则表达式还有一个非常有用的功能，那就是模式匹配（pattern match），不仅可以查找所有匹配的的字符串，还可以直接提取分组的内容，请看下面api手册的例子：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-scala" data-lang="scala"><span class="k">val</span> <span class="n">date</span> <span class="k">=</span> <span class="s">&#34;&#34;&#34;(\d\d\d\d)-(\d\d)-(\d\d)&#34;&#34;&#34;</span><span class="o">.</span><span class="n">r</span> <span class="c1">//可以直接使用.r生成一个Regex对象
</span><span class="c1"></span><span class="s">&#34;2004-01-20&#34;</span> <span class="k">match</span> <span class="o">{</span>
  <span class="k">case</span> <span class="n">date</span><span class="o">(</span><span class="n">year</span><span class="o">,</span> <span class="n">month</span><span class="o">,</span> <span class="n">day</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="s">s&#34;</span><span class="si">$year</span><span class="s"> was a good year for PLs.&#34;</span>
<span class="o">}</span>

<span class="k">=&gt;</span>
<span class="mi">2004</span> <span class="n">was</span> <span class="n">a</span> <span class="n">good</span> <span class="n">year</span> <span class="k">for</span> <span class="nc">PLs</span><span class="o">.</span>
</code></pre></td></tr></table>
</div>
</div><p>可以看到上面的例子直接把年份的分组提取了出来，month, day同样也可以提取出来。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">scala.util.matching.Regex</span>
<span class="s">&#34;2004-01-20&#34;</span> <span class="k">match</span> <span class="o">{</span>
  <span class="k">case</span> <span class="n">date</span><span class="o">(</span><span class="k">_</span><span class="o">*)</span> <span class="k">=&gt;</span> <span class="s">&#34;It&#39;s a date!&#34;</span>
<span class="o">}</span>

<span class="k">=&gt;</span>
<span class="nc">It</span>&#39;s <span class="n">a</span> <span class="n">data</span><span class="o">!</span>
</code></pre></td></tr></table>
</div>
</div><p>可以忽略匹配到的参数。</p>
<p>上面仅当字符串为XXXX-XX-XX的时候才可以使用，如果一个字符串不仅仅是只是一个年份，模式匹配是否也可以提取呢？答案是Yes，使用unanchored就可以做到，看下面的例子：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-scala" data-lang="scala"><span class="k">val</span> <span class="n">date</span> <span class="k">=</span> <span class="s">&#34;&#34;&#34;(\d\d\d\d)-(\d\d)-(\d\d)&#34;&#34;&#34;</span><span class="o">.</span><span class="n">r</span>
<span class="k">val</span> <span class="n">embeddedDate</span> <span class="k">=</span> <span class="n">date</span><span class="o">.</span><span class="n">unanchored</span>
<span class="s">&#34;Date: 2004-01-20 17:25:18 GMT (10 years, 28 weeks, 5 days, 17 hours and 51 minutes ago)&#34;</span> <span class="k">match</span> <span class="o">{</span>
  <span class="k">case</span> <span class="n">embeddedDate</span><span class="o">(</span><span class="s">&#34;2004&#34;</span><span class="o">,</span> <span class="s">&#34;01&#34;</span><span class="o">,</span> <span class="s">&#34;20&#34;</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="s">&#34;A Scala is born.&#34;</span>
<span class="o">}</span>

<span class="k">=&gt;</span>
<span class="n">A</span> <span class="nc">Scala</span> <span class="n">is</span> <span class="n">born</span><span class="o">.</span>
</code></pre></td></tr></table>
</div>
</div><p>给正则表达式的分组命名：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">scala.util.matching.Regex</span>
<span class="k">val</span> <span class="n">str</span> <span class="k">=</span> <span class="s">&#34;2016-08-23&#34;</span>
<span class="k">val</span> <span class="n">date</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">Regex</span><span class="o">(</span><span class="s">&#34;&#34;&#34;(\d\d\d\d)-(\d\d)-(\d\d)&#34;&#34;&#34;</span><span class="o">,</span> <span class="s">&#34;year&#34;</span><span class="o">,</span> <span class="s">&#34;month&#34;</span><span class="o">,</span> <span class="s">&#34;day&#34;</span><span class="o">)</span>
<span class="k">val</span> <span class="n">matches</span> <span class="k">=</span> <span class="n">date</span><span class="o">.</span><span class="n">findAllIn</span><span class="o">(</span><span class="n">str</span><span class="o">)</span>
<span class="n">println</span><span class="o">(</span><span class="s">&#34;year: &#34;</span> <span class="o">+</span> <span class="n">matches</span><span class="o">.</span><span class="n">group</span><span class="o">(</span><span class="s">&#34;year&#34;</span><span class="o">))</span>

<span class="k">=&gt;</span>
<span class="n">year</span><span class="k">:</span> <span class="err">2016</span>
</code></pre></td></tr></table>
</div>
</div><p>Scala的正则表达式默认使用单行模式去匹配文本内容</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">scala.util.matching.Regex</span>
<span class="k">val</span> <span class="n">str</span> <span class="k">=</span> <span class="s">&#34;abc\nefg&#34;</span>
<span class="k">val</span> <span class="n">regex</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">Regex</span><span class="o">(</span><span class="s">&#34;^[a-z].*&#34;</span><span class="o">)</span>
<span class="n">println</span><span class="o">(</span><span class="n">regex</span><span class="o">.</span><span class="n">findAllIn</span><span class="o">(</span><span class="n">str</span><span class="o">).</span><span class="n">toList</span><span class="o">)</span>

<span class="k">=&gt;</span>
<span class="nc">List</span><span class="o">(</span><span class="n">abc</span><span class="o">)</span>
</code></pre></td></tr></table>
</div>
</div><p>只是匹配到了<code>abc</code>，把正则表达式加上<code>s</code>选项那么<code>.</code>就可以匹配到换行符。结果就<code>abc\nefg</code>可以全部匹配到。如果想以多选的方式匹配一段文本内容，那么就需要使用<code>m</code>选项：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">scala.util.matching.Regex</span>
<span class="k">val</span> <span class="n">str</span> <span class="k">=</span> <span class="s">&#34;abc\nefg&#34;</span>
<span class="k">val</span> <span class="n">regex</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">Regex</span><span class="o">(</span><span class="s">&#34;(?m)^[a-z].*&#34;</span><span class="o">)</span>
<span class="n">println</span><span class="o">(</span><span class="n">regex</span><span class="o">.</span><span class="n">findAllIn</span><span class="o">(</span><span class="n">str</span><span class="o">).</span><span class="n">toList</span><span class="o">)</span>

<span class="k">=&gt;</span>
<span class="nc">List</span><span class="o">(</span><span class="n">abc</span><span class="o">,</span> <span class="n">efg</span><span class="o">)</span>
</code></pre></td></tr></table>
</div>
</div><p>测试一个字符串是否匹配一个模式：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">val str = &#34;abc123&#34;
val pattern = &#34;[a-z]{3}[0-9]{3}&#34;
println(str.matches(pattern))

=&gt;
true
</code></pre></td></tr></table>
</div>
</div>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">Kevin Jiang</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2016-08-23
        
    </span>
  </p>
  
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content"><a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank">CC BY-NC-ND 4.0</a></span>
  </p>
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/wechat_pay_1242x1242.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/alipay_600x600.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/scala/">Scala</a>
          <a href="/tags/scala%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/">Scala正则表达式</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/%E4%BB%8Evirtualbox%E5%88%9B%E5%BB%BAvagrant-box/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">从virtualbox创建vagrant box</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/clojure/clojurescript%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B8%80hello-world%E4%B8%8Erepl/">
            <span class="next-text nav-default">ClojureScript学习笔记一（Hello World与REPL）</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wenlin1988@126.com" class="iconfont icon-email" title="email"></a>
      <a href="https://www.linkedin.com/in/%E6%96%87%E6%9E%97-%E8%92%8B-0a3204126/" class="iconfont icon-linkedin" title="linkedin"></a>
      <a href="https://github.com/kevindragon" class="iconfont icon-github" title="github"></a>
  <a href="http://kevinjiang.info/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 -
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy;
    2015 -
    2023<span class="heart"><i class="iconfont icon-heart"></i></span><span>Kevin Jiang</span>
    <a href="https://beian.miit.gov.cn/" target="_blank">湘ICP备2022022745号</a>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        tags: 'ams',
        }
    };
  </script>
  <script type="text/javascript" async src="/lib/mathjax/es5/tex-mml-chtml.js"></script>

<script id="baidu_analytics">
  var _hmt = _hmt || [];
  (function() {
    if (window.location.hostname === 'localhost') return;
    var hm = document.createElement("script"); hm.async = true;
    hm.src = "https://hm.baidu.com/hm.js?b73ff6d4afc4af9e582d8a5dc068bab9";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>






</body>
</html>
